//管理员
(function(){
	var db_table_name = "admin";
	var db = require("../db/index");
	var md5 = require("../_include/md5");

	//构造函数
	var _con = function (info){
		var self = this;
		//身份编号
		self._id = "AM_"+Math.random().toString(16).substr(2)+(+new Date).toString(16);
		//操作记录
		self._his = [];
		//额外信息
		self.info = info;
		//密码非明文存储
		self.info.password = md5.md5_2(self.info.password);

		//保存管理员信息
		db.insert(db_table_name,self);
		//日志打印
		console.log("注册了一个管理员：",info.name);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};
	_con.login = function(admin_info){
		admin_info.name = admin_info.name.toLowerCase();
		var mybey_admin = db.find_one(db_table_name,{
			info:{
				name:admin_info.name
			}
		});
		if (!mybey_admin) {
			console.log("找不到管理员名为：“",admin_info.name,"”的管理员！");
			throw "找不到指定的管理员";
		}else if(mybey_admin.info.password!==md5.md5_2(admin_info.password)){
			console.log("密码错误！");
			throw "密码错误";
		}else{
			console.log(admin_info.name,"登陆成功");
			return mybey_admin;
		}
	};
}());
